#include <stdio.h>
#include <math.h>


size_t str_len(const char *str)
{
    size_t len = 0;
    while (*str++){
        ++len;
    }
    return len;
}

char* str_cpy(char *d, const char *s){
    char *tmp = d;

    while (1) {
        if (!(*d++ = *s++)) {
            break;
        }
    }
    return tmp;
}

char* str_n_cpy(char *d, const char *s, unsigned n){
    char *tmp = d;

    while (n) {
        if (!(*d++ = *s++)) {
            break;
        }
        --n;
    }
    return tmp;
}

char* str_cat(char *a, const char *b){
    char *tmp = a;

    while (*a) *a++;

    while (*a++ = *b++);

    return tmp;
}

char* str_n_cat(char *a, const char *b, unsigned n) {
    char *tmp = a;

    while (*a) *a++;

    while (n) {
        if (!(*a++ = *b++)) {
            break;
        }
        --n;
    }

    return tmp;
}

int str_cmp(char *a, char *b) {
    while (*a) {
        if (*a++ != *b++) {
            return *a - *b;
        }
    }

    return *b ? 1 : 0;
}

int str_n_cmp(char *a, char *b, unsigned n) {
    while (*a) {
        if (!n) return 0;

        if (*a++ != *b++) {
            return *a - *b;
        }

        n--;
    }

    return *b ?1 :0;
}

int a_to_i(const char *a) {
    int tmp[100] = {0};
    int c = -1;
    int ret = 0;

    while (*a) {
        if (*a < '0' || *a > '9') {
            break;
        }

        tmp[++c] = *a-48;

        ++a;
    }

    for (int i =0; i <=c; ++i) {
        ret += tmp[i] * pow(10, c-i);
    }

    return ret;
}

double a_to_f(const char *a)
{
    int tmp[100] = {0};

    //tmp 赋值下标
    int c = -1;

    //小数是第几位(从0开始算起)
    int period = -1;

    double ret = 0;

    while (*a) {

        //记下小数第一位的下标(小数点不点位,并且可能不存在小数)
        //只有第一个小数点是有效的
        if (*a == '.' && !~period) {
            period = c+1;

        //结束
        } else if (*a < '0' || *a > '9') {
            break;

        } else {
            tmp[++c] = *a - 48;
        }

        ++a;
    }

    //如果字符串中没有小数点
    if (!~period && (period = c+1));

    //开始统计整数部分
    for (int i=0; i<period; ++i) {
        ret += tmp[i] * pow(10, period - i -1);
    }

    //开始统计小数部分
    for (int i=period; i <= c; ++i) {
        ret += tmp[i] * pow(10, -(i-period+1));
    }

    return ret;
}

/*
 *@deprecated use del_num_on_str2
 * */
char* del_num_on_str(const char *a) {
    //这里如果不使用static变量,那么在函数外面该字符串就回收了
    //但是如果使用static,所有调用的方法最后都得到同样的值
    //所以应该让用户传一个保存修改结果的指针
    static char ret[127] ="";

    int i =0;

    char *p = ret;

    printf("p:%p %s\n", p,p);

    while (*a) {
        if (*a <'0' || *a > '9' ) {
            ret[i] = *a;
            i++;
        }
        ++a;

    }
    printf("p:%p %s\n", p, p);
    return p;
}

char* del_num_on_str2(char *d, const char *s) {
    char *r = d;

    while (*s) {
        if (*s < '0' || *s > '9') {
            puts("aaa");
            *d++ = *s;
        }
        s++;
    }

    return r;
}


void main(void) {
    char *str = "helloWorld";

    printf("length of string \"%s\" : %lu\n", str, str_len(str));

    char str2[127] = {0};
    printf("copy %s, result : %s\n", str, str_cpy(str2, str));

    char str3[127] = {0};
    char str4[127] = {0};
    char str5[127] = {0};
    printf(
        "\ncopy %s,\n to index 2: %s\n to index 5: %s\n to index 100:%s\n"
        ,str
        ,str_n_cpy(str3, str, 2)
        ,str_n_cpy(str4, str, 5)
        ,str_n_cpy(str5, str, 100)
        );


    char str6[127] = "abc";
    printf("\n\n str_cat:\noriginal: %s\n",str6);
    printf("result: %s\n", str_cat(str6, "HaHaHa"));

    char str7[127] = "abc";
    printf("\n\nstr_n_cat:\noriginal:%s\n", str7);
    printf("result:%s\n", str_n_cat(str7, "HaHaHa", 3));

    char *str8 = "43258";
    char *str9 = "14a";
    char *str10 = "14.9";
    char *fstr = "string [%s] convent to int: %d\n";

    puts("\n\na_to_i:\n");
    printf(fstr, str8, a_to_i(str8));
    printf(fstr, str9, a_to_i(str9));
    printf(fstr, str10, a_to_i(str10));

    puts("\n\na_to_f:\n");
    char *str11 = "13.456";
    char *str12 = "12354.3";
    char *str13 = "1244";
    char *str14 = "1.234567a";
    char *str15 = "12.34.55.66";
    fstr = "string [%s] convent to float: %f\n";

    printf(fstr, str11, a_to_f(str11));
    printf(fstr, str12, a_to_f(str12));
    printf(fstr, str13, a_to_f(str13));
    printf(fstr, str14, a_to_f(str14));
    printf(fstr, str15, a_to_f(str15));

    puts("\n\ndel_num_on_str:\n");
    char *str16 = "AB12C9";
    char *str17 = "12CCB34E";

    char str18[127] = {0};
    char str19[127] = {0};

    printf(
            "%s => %s; \n%s => %s\n"
            ,str16
            ,del_num_on_str2(str18 ,str16)
            ,str17
            ,del_num_on_str2(str19, str17)
          );


    char str20[127] = {0};
    str20 = 'A';

    printf("\n\nstr20: %s\n", str20);
}



